package com.im.partchat.topic;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.im.partchat.R;
import com.im.partchat.topic.bean.TopicBean;
import com.im.partchat.topic.widget.PullToRefreshBase;
import com.im.partchat.topic.widget.PullToRefreshPagedListView;

import java.util.HashMap;
import java.util.List;

/**
 * 动态 tab界面
 * OnMyActionMessageGetListener，来新穿透消息监听（新粉丝，新评论）
 * OnMyActionMessageHadReadListener，新穿透消息已读监听（新粉丝已被读，新评论已被读）
 */
public class TopicFragment extends BaseTopicFragment {

	private String targetApi;//选择菜单后要访问不同的接口api
	private String orderString;//排序方式，@""表示默认，praise表示按点赞排序
	
	private View mEmptyLayout;//内容为空显示的emptyview
	private View locationFailLayout;//定位失败的emptyview
	private PullToRefreshPagedListView pullToRefreshView;
	private TextView unReadReviewTextView;
	private LinearLayout unReadTopicReviewLL;
	private LinearLayout writeTopic;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_topic, container, false);
		initView(view);
		initListener();
		return view;
	}

	@Override
	protected void initView(View view) {
		// TODO Auto-generated method stub
		super.initView(view);
		initProgressDialog();
	    targetApi = "topic/getlist";
	    orderString = "";
		writeTopic = (LinearLayout) view.findViewById(R.id.ly_write);
		unReadTopicReviewLL = (LinearLayout) view.findViewById(R.id.unReadTopicReviewLL);
		unReadReviewTextView = (TextView) view.findViewById(R.id.unReadReviewTextView);

		checkUnReadActionCount(0);

		pullToRefreshView = (PullToRefreshPagedListView) view.findViewById(R.id.listView);
		listView = pullToRefreshView.getRefreshableView();
		
		mEmptyLayout = LayoutInflater.from(getActivity()).inflate(R.layout.listview_empty, null);
		locationFailLayout = LayoutInflater.from(getActivity()).inflate(R.layout.listview_empty, null);
		TextView textViewMessage = (TextView)locationFailLayout.findViewById(R.id.textViewMessage);
		textViewMessage.setText("定位失败！\n请检查是否开启定位权限");
        textViewMessage.setVisibility(View.GONE);
		pullToRefreshView.setRefreshing();
		refresh();
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		super.initListener();
		pullToRefreshView
				.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

					@Override
					public void onPullDownToRefresh() {
						//当前查询的是附近的动态，需要先定位
						refresh();
					}

					@Override
					public void onPullUpToRefresh() {

					}
				});

		writeTopic.setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getActivity(), TopicReleaseActivity.class);
						startActivityForResult(intent, 1);
					}
				});
		unReadTopicReviewLL.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
//				Intent i = new Intent(TopicFragment.this,
//						CommentUnReadActivity.class);
//				startActivity(i);
			}
		});
	}
	
	private void checkUnReadActionCount(int unReadActionCount) {
		// TODO Auto-generated method stub
		unReadReviewTextView.setText("" + unReadActionCount + "条新评论（赞）");
		unReadTopicReviewLL.setVisibility(unReadActionCount == 0 ? View.GONE : View.VISIBLE);
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case TopicReleaseActivity.RELEASE_SUCCESS:
			pullToRefreshView.setRefreshing();
			refresh();
			break;
		default:
			break;
		}
	}


	@Override
	public void onNetWorkFinish(Message msg) {
		// TODO Auto-generated method stub
		pullToRefreshView.onRefreshComplete();
	}

	@Override
	public void onRefreshNetWorkSuccess(List<TopicBean> list) {
		// TODO Auto-generated method stub
		pullToRefreshView.setEmptyView(list.isEmpty() ? mEmptyLayout : null);
	}

	/**
	 * 表示调用的接口是getlist（默认）
	 * 子类可以修改，比如返回getpraiselist 表示查询我赞的动态
	 */
	@Override
	protected String getApi(){
		return targetApi;
	}
	
	/**
	 * 子类可以重写此类来添加请求网络时候的自定义参数
	 */
	@Override
	protected HashMap<String,String> getRequireHashMap(){
		HashMap<String,String> params =  new HashMap<>();
//		if (currentLocationBean != null) {
//			params.put("latitude", ""+currentLocationBean.getLatitude());
//			params.put("longitude", ""+currentLocationBean.getLongitude());
//		}
		params.put("order",orderString);
		return params;
	}
	
	//重新请求数据
	@Override
	protected void refresh() {
        new Thread(run).start();
	}

}
